

import os
import sys
import subprocess


# download from conan-center
def download(package):
    # conan info package@ -r=conan-center
    ret = subprocess.run('conan info '+package+'@ -r=conan-center')
    return ret.returncode

# upload to local
def upload(package, local_r):
    # conan upload package -r=local_r
    ret = subprocess.run('conan upload -c '+package+' -r='+local_r)
    return ret.returncode

# load url list
def load_package_list(f):
    urls = []
    with open(f, 'r') as fp:
        line = fp.readline()
        while line:
            if line.find('Package:')==0:
                urls.append(line.replace('Package: ',''))
            line = fp.readline()
    return urls


if len(sys.argv) != 3:
    print("python download.py packagelist.txt local_conan_center")
    sys.exit(-1)

url_list = load_package_list(sys.argv[1])
to_dir = sys.argv[2]

for url in url_list:
    url = url.replace('\n', '')
    url = url.replace('\r', '')
    print("package:", url)
    re = download(url)
    if re != 0:
        print('download fail!')
        pass
    else:
        re = upload(url, to_dir)
        if re != 0:
            print("upload fail!")
        else:
            print("upload success!")
